package ibm_package;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Registration extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String fName , lName , faculty , field ,driver , connector;
	private ServletConfig config;
    private Connection conn;
    private Statement stmt;
    public Registration() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init(ServletConfig conf) throws ServletException{
    	config = conf;
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// get parameters Data
		
		fName = request.getParameter("fName");
		lName = request.getParameter("lName");
		faculty = request.getParameter("faculty");
		field = request.getParameter("field");
		PrintWriter out = response.getWriter();
		
		// get config info for database connection
		driver = "com.mysql.jdbc.Driver";
		connector = "jdbc:mysql://localhost/ibm_training";
		// jdbc process
		
		try{
			// load jdbc driver
			System.out.println(driver);
			Class.forName(driver);
			conn = (Connection)DriverManager.getConnection(connector , "root" , "");
			
			// query statement
			
			stmt = (Statement) conn.createStatement();
			
			String query = "INSERT INTO trainee(Fname,Lname,faculty,field) VALUES('"+fName+"', '"+lName+"', '"+faculty+"', '"+field+"')";
			System.out.println(query);
		//			String query = "INSERT INTO trainee(Fname,Lname,faculty,field) " +
//	                   "VALUES ('nader', 'Zara', 'Ali', 'web app')";
			stmt.executeUpdate(query);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	//	out.println(driver + " " + connector);
	//	out.println(fName + "  " + lName + " " + faculty + "  "+ field);
		
		out.println("Succeful registered");
	}

}
